﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation.Forms
{
	public partial class MainForm : System.Windows.Forms.Form
	{
		public MainForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				this.CalculateAreaBox();
				return;
			}
			base.DefWndProc(ref m);
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.SubFormsLoad();
			this.InitializeBuildingParamterList();
			this.LoadDlgData();
		}

		private void SubFormsLoad()
		{
			if (this.m_FormulaMethodSubForm == null)
			{
				this.m_FormulaMethodSubForm = new FormulaMethodSubForm(base.Handle);
				this.m_FormulaMethodSubForm.TopLevel = false;
				this.panelSubForm.Controls.Add(this.m_FormulaMethodSubForm);
			}
			if (this.m_GraphicMethodSubForm == null)
			{
				this.m_GraphicMethodSubForm = new GraphicMethodSubForm(this.m_Revit.Application.ActiveUIDocument.Document);
				this.m_GraphicMethodSubForm.TopLevel = false;
				this.panelSubForm.Controls.Add(this.m_GraphicMethodSubForm);
			}
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
            if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				string text = "";
				if (!this.CanBeExport(ref text))
				{
					MessageBox.Show(text, "盈建科提示");
					e.Cancel = true;
					return;
				}
			}
			ReferenceCurveDrawingKit.ReleaseResources(this.m_Revit.Application.ActiveUIDocument.Document);
			this.SaveDlgData();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "LightningCalculationMainForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("X", base.Location.X.ToString());
			xmlHelper.SetValue("Y", base.Location.Y.ToString());
			xmlHelper.SetValue("rdbFormulaMethod", this.rdbFormulaMethod.Checked.ToString());
			xmlHelper.SetValue("rdbGraphicMethod", this.rdbGraphicMethod.Checked.ToString());
			xmlHelper.SetValue("tbThunderstormDays", this.tbThunderstormDays.Text);
			xmlHelper.SetValue("tbCorrectionCoefficient", this.tbCorrectionCoefficient.Text);
			xmlHelper.SetValue("cmbBuildingParamter", this.cmbBuildingParamter.SelectedIndex.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "LightningCalculationMainForm.xml");
			if (!File.Exists(text))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.rdbFormulaMethod.Checked = true;
				this.m_ThunderstormDays = "35.2";
				this.tbThunderstormDays.Text = this.m_ThunderstormDays;
				this.m_CorrectionCoefficient = "1";
				this.tbCorrectionCoefficient.Text = this.m_CorrectionCoefficient;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			int x = Convert.ToInt32(xmlHelper.GetValue("X"));
			int y = Convert.ToInt32(xmlHelper.GetValue("Y"));
			base.Location = new System.Drawing.Point(x, y);
			if (xmlHelper.GetValue("rdbFormulaMethod") == "True")
			{
				this.rdbFormulaMethod.Checked = true;
			}
			else
			{
				this.rdbFormulaMethod.Checked = false;
			}
			if (xmlHelper.GetValue("rdbGraphicMethod") == "True")
			{
				this.rdbGraphicMethod.Checked = true;
			}
			else
			{
				this.rdbGraphicMethod.Checked = false;
			}
			this.m_ThunderstormDays = xmlHelper.GetValue("tbThunderstormDays");
			this.tbThunderstormDays.Text = this.m_ThunderstormDays;
			this.m_CorrectionCoefficient = xmlHelper.GetValue("tbCorrectionCoefficient");
			this.tbCorrectionCoefficient.Text = this.m_CorrectionCoefficient;
			this.cmbBuildingParamter.SelectedIndex = Convert.ToInt32(xmlHelper.GetValue("cmbBuildingParamter"));
		}

		private void btnAnalyseArea_Click(object sender, EventArgs e)
		{
			if (this.rdbFormulaMethod.Checked)
			{
				LightningCalculationManager lightningCalculationManager = new LightningCalculationManager(this.m_Revit);
				string text = "";
				double num = 0.0;
				double num2 = 0.0;
				double num3 = 0.0;
				if (lightningCalculationManager.ProfileCalculateByFormulaMethod(ref num, ref num2, ref num3, ref text) != null)
				{
					MessageBox.Show(text, "盈建科提示");
					return;
				}
				this.m_FormulaMethodSubForm.BuildingLength = Common.FeetToMM(num) / 1000.0;
				this.m_FormulaMethodSubForm.BuildingWidth = Common.FeetToMM(num2) / 1000.0;
				this.m_FormulaMethodSubForm.BuildingHeight = Common.FeetToMM(num3) / 1000.0;
				this.CalculateAreaBox(num, num2, num3);
				return;
			}
			else
			{
				LightningCalculationManager lightningCalculationManager2 = new LightningCalculationManager(this.m_Revit);
				string text2 = "";
				List<Level> selectLevels = this.m_GraphicMethodSubForm.GetSelectLevels();
				LevelProfiles levelProfiles = null;
				if (lightningCalculationManager2.ProfileCalculateByGraphicMethod(selectLevels, ref levelProfiles, ref text2) != null)
				{
					MessageBox.Show(text2, "盈建科提示");
					return;
				}
				double num4 = Common.FeetToMM(Common.FeetToMM(levelProfiles.Area())) / 1000.0 / 1000.0 / 1000.0 / 1000.0;
				this.tbEquivalentArea.Text = num4.ToString("f4");
				return;
			}
		}

		private void CalculateAreaBox(double length, double width, double height)
		{
			double num = 0.0;
			LightningCalculator.FormulaMethodCalculateArea(this.m_FormulaMethodSubForm.GetEnvironmentalFactors(), length, width, height, ref num, ref this.m_AeCalculateFormulaDetailed);
			this.tbEquivalentArea.Text = num.ToString("f4");
		}

		private void CalculateAreaBox()
		{
			double length = Common.MMToFeet(this.m_FormulaMethodSubForm.BuildingLength * 1000.0);
			double width = Common.MMToFeet(this.m_FormulaMethodSubForm.BuildingWidth * 1000.0);
			double height = Common.MMToFeet(this.m_FormulaMethodSubForm.BuildingHeight * 1000.0);
			this.CalculateAreaBox(length, width, height);
		}

		private double GetMaxHeight()
		{
			if (this.rdbFormulaMethod.Checked)
			{
				return this.m_FormulaMethodSubForm.MaxHeight;
			}
			List<Level> selectLevels = this.m_GraphicMethodSubForm.GetSelectLevels();
			if (selectLevels.Count == 0)
			{
				return 0.0;
			}
			return Common.FeetToMM(selectLevels[selectLevels.Count - 1].Elevation) / 1000.0;
		}

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			string text = "";
			if (!this.CanBeCalculate(ref text))
			{
				MessageBox.Show(text, "盈建科提示");
				return;
			}
			double ae = Convert.ToDouble(this.tbEquivalentArea.Text);
			double k = Convert.ToDouble(this.tbCorrectionCoefficient.Text);
			double ng = Convert.ToDouble(this.tbDensity.Text);
			double count = 0.0;
			LightningCalculator.CalculateLightning(ae, k, ng, ref count);
			this.tbLightningCount.Text = count.ToString();
			double maxHeight = this.GetMaxHeight();
			this.tbLightningProtectionCategory.Text = this.GetLightningProtectionCategory(count, maxHeight);
		}

		private void btnReport_Click(object sender, EventArgs e)
		{
			string text = "";
			if (!this.CanBeExport(ref text))
			{
				MessageBox.Show(text, "盈建科提示");
				return;
			}
			new CalculateReport(this.GetCalculationData()).ExportReport();
		}

		private void btnThunderstormDaysSetting_Click(object sender, EventArgs e)
		{
			ThunderstormDaysSettingForm thunderstormDaysSettingForm = new ThunderstormDaysSettingForm();
			if (thunderstormDaysSettingForm.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			string getThunderstormDays = thunderstormDaysSettingForm.GetThunderstormDays;
			if (getThunderstormDays.CompareTo("") != 0)
			{
				this.tbThunderstormDays.Text = getThunderstormDays;
			}
		}

		private void btnCorrectionCoefficientSetting_Click(object sender, EventArgs e)
		{
			CorrectionCoefficientForm correctionCoefficientForm = new CorrectionCoefficientForm();
			if (correctionCoefficientForm.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			this.tbCorrectionCoefficient.Text = correctionCoefficientForm.GetK;
		}

		private void tbThunderstormDays_TextChanged(object sender, EventArgs e)
		{
			try
			{
				double num = Convert.ToDouble(this.tbThunderstormDays.Text);
				if (Geometry.LessThan(num, 0.0))
				{
					this.tbThunderstormDays.Text = this.m_ThunderstormDays;
					num = Convert.ToDouble(this.m_ThunderstormDays);
					this.tbDensity.Text = (num / 10.0).ToString();
				}
				else
				{
					this.tbDensity.Text = (num / 10.0).ToString();
					this.m_ThunderstormDays = this.tbThunderstormDays.Text;
				}
			}
			catch (Exception)
			{
				this.tbThunderstormDays.Text = this.m_ThunderstormDays;
				double num2 = Convert.ToDouble(this.m_ThunderstormDays);
				this.tbDensity.Text = (num2 / 10.0).ToString();
			}
		}

		private void tbCorrectionCoefficient_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (Geometry.LessThan(Convert.ToDouble(this.tbCorrectionCoefficient.Text), 0.0))
				{
					this.tbCorrectionCoefficient.Text = this.m_CorrectionCoefficient;
				}
				else
				{
					this.m_CorrectionCoefficient = this.tbCorrectionCoefficient.Text;
				}
			}
			catch (Exception)
			{
				this.tbCorrectionCoefficient.Text = this.m_CorrectionCoefficient;
			}
		}

		private string GetLightningProtectionCategory(double count, double height)
		{
			string result = "非第三类防雷";
			int selectedIndex = this.cmbBuildingParamter.SelectedIndex;
			if (selectedIndex > 0 && selectedIndex < 4)
			{
				result = "第一类防雷";
			}
			else if (selectedIndex > 4 && selectedIndex < 13)
			{
				result = "第二类防雷";
			}
			else if (selectedIndex > 13 && selectedIndex < 15)
			{
				result = "第三类防雷";
			}
			else if (selectedIndex == 16)
			{
				if (Geometry.GreaterThan(count, 0.05))
				{
					result = "第二类防雷";
				}
				else if (Geometry.Greaterthan_Or_Equal(count, 0.01) && Geometry.Lessthan_Or_Equal(count, 0.05))
				{
					result = "第三类防雷";
				}
			}
			else if (selectedIndex == 17)
			{
				if (Geometry.GreaterThan(count, 0.25))
				{
					result = "第二类防雷";
				}
				else if (Geometry.Greaterthan_Or_Equal(count, 0.05) && Geometry.Lessthan_Or_Equal(count, 0.25))
				{
					result = "第三类防雷";
				}
			}
			else if ((Geometry.GreaterThan(count, 15.0) && Geometry.Greaterthan_Or_Equal(height, 15.0)) || (Geometry.Lessthan_Or_Equal(count, 15.0) && Geometry.Greaterthan_Or_Equal(height, 20.0)))
			{
				result = "第三类防雷";
			}
			return result;
		}

		private void InitializeBuildingParamterList()
		{
			this.cmbBuildingParamter.Items.Clear();
			this.cmbBuildingParamter.Items.Add("第一类");
			this.cmbBuildingParamter.Items.Add("凡制造、使用或贮存火炸药及其制品的危险建筑物，因电火花而引起爆炸、爆轰，会造成巨大破坏和人身伤亡者");
			this.cmbBuildingParamter.Items.Add("具有0区或20区爆炸危险场所的建筑物");
			this.cmbBuildingParamter.Items.Add("具有1区或21区爆炸危险场所的建筑物，因电火花而引起爆炸，会造成巨大破坏和人身伤亡者");
			this.cmbBuildingParamter.Items.Add("第二类");
			this.cmbBuildingParamter.Items.Add("国家级重点文物保护的建筑物");
			this.cmbBuildingParamter.Items.Add("国家级的会堂、办公建筑物、大型展览和博览建筑物、大型火车站和飞机场、国宾馆，国家级档案馆、大型城市的重要给水泵房等特别重要的建筑物");
			this.cmbBuildingParamter.Items.Add("国家级计算中心、国际通信枢纽等对国民经济有重要意义的建筑物");
			this.cmbBuildingParamter.Items.Add("国家特级和甲级大型体育馆");
			this.cmbBuildingParamter.Items.Add("制造、使用或贮存火炸药及其制品的危险建筑物，且电火花不易引起爆炸或不致造成巨大破坏和人身伤亡者");
			this.cmbBuildingParamter.Items.Add("具有1区或21区爆炸危险场所的建筑物，且电火花不易引起爆炸或不致造成巨大破坏和人身伤亡者");
			this.cmbBuildingParamter.Items.Add("具有2区或22区爆炸危险场所的建筑物");
			this.cmbBuildingParamter.Items.Add("有爆炸危险的露天钢质封闭气罐");
			this.cmbBuildingParamter.Items.Add("第三类");
			this.cmbBuildingParamter.Items.Add("省级重点文物保护的建筑物及省级档案馆");
			this.cmbBuildingParamter.Items.Add("需条件判断类");
			this.cmbBuildingParamter.Items.Add("部、省级办公建筑物和其他重要或人员密集的公共建筑物以及火灾危险场所");
			this.cmbBuildingParamter.Items.Add("住宅、办公楼等一般性民用建筑物或一般性工业建筑物");
			this.cmbBuildingParamter.Items.Add("烟囱、水塔等孤立的高耸建筑物");
			this.cmbBuildingParamter.SelectedIndex = 1;
		}

		private void cmbBuildingParamter_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cmbBuildingParamter.SelectedIndex == 0 || this.cmbBuildingParamter.SelectedIndex == 4 || this.cmbBuildingParamter.SelectedIndex == 13 || this.cmbBuildingParamter.SelectedIndex == 15)
			{
				this.cmbBuildingParamter.SelectedIndex = this.cmbBuildingParamter.SelectedIndex + 1;
			}
		}

		private void rdbGraphicMethod_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbGraphicMethod.Checked)
			{
				this.tbEquivalentArea.Text = "";
				this.tbLightningCount.Text = "";
				this.tbLightningProtectionCategory.Text = "";
				this.m_FormulaMethodSubForm.Hide();
				this.m_GraphicMethodSubForm.ClearSelectLevels();
				this.m_GraphicMethodSubForm.Show();
			}
		}

		private bool CanBeCalculate(ref string message)
		{
			if (this.tbEquivalentArea.Text.CompareTo("") == 0)
			{
				message = "等效面积为空";
				return false;
			}
			if (this.tbCorrectionCoefficient.Text.CompareTo("") == 0)
			{
				message = "校正系数为空";
				return false;
			}
			if (this.tbDensity.Text.CompareTo("") == 0)
			{
				message = "年平均密度为空";
				return false;
			}
			return true;
		}

		private bool CanBeExport(ref string message)
		{
			if (!this.CanBeCalculate(ref message))
			{
				return false;
			}
			if (this.tbLightningCount.Text.CompareTo("") == 0)
			{
				message = "计算结果为空";
				return false;
			}
			return true;
		}

		private void rdbFormulaMethod_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbFormulaMethod.Checked)
			{
				this.tbEquivalentArea.Text = "";
				this.tbLightningCount.Text = "";
				this.tbLightningProtectionCategory.Text = "";
				this.m_FormulaMethodSubForm.BuildingLength = 0.0;
				this.m_FormulaMethodSubForm.BuildingWidth = 0.0;
				this.m_FormulaMethodSubForm.BuildingHeight = 0.0;
				this.m_FormulaMethodSubForm.Show();
				this.m_GraphicMethodSubForm.Hide();
			}
		}

		public CalculationData GetCalculationData()
		{
			return new CalculationData
			{
				IsFormulaMethod = this.rdbFormulaMethod.Checked,
				BuildingLength = this.m_FormulaMethodSubForm.BuildingLength,
				BuildingWidth = this.m_FormulaMethodSubForm.BuildingWidth,
				BuildingHeight = this.m_FormulaMethodSubForm.BuildingHeight,
				Td = Convert.ToDouble(this.tbThunderstormDays.Text),
				K = Convert.ToDouble(this.tbCorrectionCoefficient.Text),
				BuildingEnvironmentalFactors = this.m_FormulaMethodSubForm.GetEnvironmentalFactors(),
				Ae = Convert.ToDouble(this.tbEquivalentArea.Text),
				AeCalculateFormulaDetailed = this.m_AeCalculateFormulaDetailed,
				Ng = Convert.ToDouble(this.tbDensity.Text),
				N = Convert.ToDouble(this.tbLightningCount.Text),
				BuildingParamter = this.cmbBuildingParamter.Text,
				ProtectionCategory = this.tbLightningProtectionCategory.Text
			};
		}

		private ExternalCommandData m_Revit;

		private FormulaMethodSubForm m_FormulaMethodSubForm;

		private GraphicMethodSubForm m_GraphicMethodSubForm;

		private string m_ThunderstormDays = "";

		private string m_CorrectionCoefficient = "";

		private List<string> m_AeCalculateFormulaDetailed = new List<string>();
	}
}
